package com.freework.util.excel.read.service.rowToObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.beanutils.ConvertUtils;

import com.freework.freedbm.base.DTO;
import com.freework.freedbm.cfg.tablecfg.TableDataManager;
import com.freework.freedbm.dao.jdbcm.JdbcForDTO;
import com.freework.freedbm.util.DTOByCfg;
import com.freework.util.NumberUtil;
import com.freework.util.excel.read.vo.ColVO;

public class RowToDTOImpi implements RowToObjectI {
	TableDataManager manager=null;
	SetValueI[] setValue=null;
	public Object createObject() {
		return manager.newInstance();
	}
	public SetValueI[] getSetValues() {
		return setValue;
	}

	
	public void init(List<ColVO> list, Class clazz) {
		try {
			DTO dto=(DTO) clazz.newInstance();
			manager=DTOByCfg.getTableDataManager(dto);
			setValue =new SetValueI[list.size()];
			int i=0;
			for (ColVO colVO : list) {
				setValue[i]=new SetValue(manager.getField(colVO.getColname()));
				i++;
			}
			
		} catch (InstantiationException e) {
			e.printStackTrace();

			throw new RuntimeException(e);

		} catch (IllegalAccessException e) {
			e.printStackTrace();

			throw new RuntimeException(e);

		}

	}
	class SetValue extends AbstractSetValue{
		JdbcForDTO f=null;
		SetValue(JdbcForDTO f){
			this.f=f;
		}
		public void setObjValue(Object obj, Object value, ColVO vo) {
			f.setValue(obj, value);
			
		}

		@Override
		Class getClass(ColVO vo) {
			return f.getType().getReturnedClass();
		}
		
		
	}
}
